package com.yonyou.ucf.mdf.app.isvdxq.firstpart.location.service;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.type.TypeReference;
import com.yonyou.ucf.mdf.app.isvdxq.common.ApiYon;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.location.model.WarehouseExtBean;
import com.yonyou.ucf.mdf.app.isvdxq.utils.GenericResponse;
import com.yonyou.ucf.mdf.app.isvdxq.utils.PageBean;
import com.yonyou.ucf.mdf.app.isvdxq.utils.RequestTool;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service
public class WarehouseExtService {

    /**
     * 获取仓库列表
     * */
    public PageBean<WarehouseExtBean> getWarehouseList(String token, String gatewayUrl) throws IOException {
        PageBean<WarehouseExtBean> res = new PageBean<>();
        String requrl = gatewayUrl + ApiYon.getWarehoseList + "?access_token=" + token;
        Map<String, Object> params = new HashMap<>();
        // 除签名外的其他参数
        params.put("pageIndex", 1);
        params.put("pageSize", 1000);
        params.put("iUsed", "enable");
//        GenericResponse<PageBean<WarehouseBean>> response2 = RequestTool.doPost(requrl, params, new TypeReference<GenericResponse<PageBean<WarehouseBean>>>() {});
//        System.out.println(JSON.toJSONString(response2));
        String resString = RequestTool.doPost(requrl, JSON.toJSONString(params));
        GenericResponse<PageBean<WarehouseExtBean>> response = RequestTool.getBean(resString, new TypeReference<GenericResponse<PageBean<WarehouseExtBean>>>() {});

        res.setPageIndex(Integer.parseInt(params.get("pageIndex").toString()));
        res.setPageSize(Integer.parseInt(params.get("pageSize").toString()));
        if (response.getCode().equals("200")) {
            PageBean<WarehouseExtBean> page = response.getData();
            res = page;
        } else {
            res.setRecordList(new ArrayList<>());
        }
        return res;
    }
}
